package com.flight.trans.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.flight.trans.R;
import com.flight.trans.activity.NFCActivity;
import com.flight.trans.activity.TugNFCActivity;
import com.flight.trans.base.BaseFragment;
import com.flight.trans.bean.ChannelBean.ChannelData;
import com.flight.trans.bean.TugBean;
import com.flight.trans.bean.NfcBean;
import com.flight.trans.bean.StorageAreaBean.StorageAreaData;
import com.flight.trans.global.Constant;
import com.flight.trans.http.NetworkUtils;
import com.flight.trans.http.network.OkGoBackListener;
import com.flight.trans.util.JsonUtils;
import com.flight.trans.util.NfcUtils;
import com.flight.trans.util.ProgressUtils;
import com.flight.trans.util.ToastUtils;
import com.flight.trans.view.HeaderTitleView;
import com.flight.trans.view.dialog.AlertNFCDialog;
import com.flight.trans.view.dialog.TugDialog;
import com.flight.trans.view.dialog.NFCContentDialog;
import com.flight.trans.view.dialog.StorageAreaDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 进港入库
 */
public class WriteNFCFragment extends BaseFragment {

    private HeaderTitleView mHeaderView ;
    private TextView mTvNfcTug ,//
            mTvNfcPallet ,//
            mTvOldTug ,//重刷
            mTvLeavePortWarehousing ,//
            mTvLeavePortDuplicate ,//
            mTvLeavePortArea ,//
            mTvEnterPortTakeChannel ,//
            mTvEnterPortArea ;//

    private List<ChannelData> channelDataList ;

    @Override
    public int setContentView() {
        return R.layout.fragment_write_nfc;
    }

    @Override
    public void initView() {
        mHeaderView = mView.findViewById(R.id.mHeaderView);

        mTvNfcTug = mView.findViewById(R.id.mTvNfcTug);
        mTvNfcPallet = mView.findViewById(R.id.mTvNfcPallet);
        mTvOldTug = mView.findViewById(R.id.mTvOldTug);
        mTvLeavePortWarehousing = mView.findViewById(R.id.mTvLeavePortWarehousing);
        mTvLeavePortDuplicate = mView.findViewById(R.id.mTvLeavePortDuplicate);
        mTvLeavePortArea = mView.findViewById(R.id.mTvLeavePortArea);
        mTvEnterPortTakeChannel = mView.findViewById(R.id.mTvEnterPortTakeChannel);
        mTvEnterPortArea = mView.findViewById(R.id.mTvEnterPortArea);
    }

    @Override
    public void initData() {
        mHeaderView.addOnHeaderListener(OnHeaderListener);

        mTvNfcTug.setOnClickListener(this::onClickItem);
        mTvNfcPallet.setOnClickListener(this::onClickItem);
        mTvOldTug.setOnClickListener(this::onClickItem);
        mTvLeavePortWarehousing.setOnClickListener(this::onClickItem);
        mTvLeavePortDuplicate.setOnClickListener(this::onClickItem);
        mTvLeavePortArea.setOnClickListener(this::onClickItem);
        mTvEnterPortTakeChannel.setOnClickListener(this::onClickItem);
        mTvEnterPortArea.setOnClickListener(this::onClickItem);
    }

    @Override
    public void initConnect() {
        ProgressUtils.dismissProgress();
    }

    /**
     * 测试点击事件
     */
    private HeaderTitleView.OnHeaderListener OnHeaderListener = new HeaderTitleView.OnHeaderListener(){
        @Override
        public void onClick(int resource) {
            startNFCIntent(0 , "" , 0);
        }
    };

    public void onClickItem(View view){
        switch (view.getId()){
            case R.id.mTvNfcTug :
                // 拖斗
                TugDialog.getInstance(mActivity)
                        .setTugType(0)
                        .setOnItemClickListener(new TugDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(TugBean.TugData tugData) {
                                startNFCIntent(tugData.getId() , tugData.getCode() , Constant.Port_Tug);
                            }
                        }).show();
                break;
            case R.id.mTvNfcPallet :
                // 托盘
                TugDialog.getInstance(mActivity)
                        .setTugType(1)
                        .setOnItemClickListener(new TugDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(TugBean.TugData tugData) {
                                startNFCIntent(tugData.getId() , tugData.getCode() , Constant.Port_Tug);
                            }
                        }).show();
                break;
            case R.id.mTvOldTug:
                // 重刷
                turnActForResult(TugNFCActivity.class);
                break;
            case R.id.mTvLeavePortWarehousing :
                // 出港入库通道
                NetworkUtils.getCheckChannel(new OkGoBackListener(mActivity){
                    @Override
                    public void onSuccess(Object body) {
                        channelDataList = (List<ChannelData>) body;

                        showNFCDialog(initChannelDataList(channelDataList) ,1 , Constant.Leave_Port_Warehousing_Channel);
                    }
                }, Constant.LeavePortWarehousing);
                break;
            case R.id.mTvLeavePortDuplicate :
                // 出港复重通道
                NetworkUtils.getCheckChannel(new OkGoBackListener(mActivity){
                    @Override
                    public void onSuccess(Object body) {
                        channelDataList = (List<ChannelData>) body;

                        showNFCDialog(initChannelDataList(channelDataList) , 1 , Constant.Leave_Port_Duplicate_Channel);
                    }
                }, Constant.LeavePortDuplicate);
                break;
            case R.id.mTvLeavePortArea :
                // 出港库房区域
                StorageAreaDialog.getInstance(mActivity , Constant.LeavePortStorage)
                        .setSingleSelect(true)
                        .setConfirmButton(new StorageAreaDialog.OnStorageOnClickListener() {
                            @Override
                            public void onClick(List<StorageAreaData> storageAreaList) {
                                startNFCIntent(storageAreaList.get(0).getId() ,storageAreaList.get(0).getName() , Constant.Leave_Port_Area);
                            }
                        })
                        .show();
                break;
            case R.id.mTvEnterPortTakeChannel :
                // 进港提货通道
                ToastUtils.showToast("进港提货通道暂未开放");
                break;
            case R.id.mTvEnterPortArea :
                // 进港库房区域
                StorageAreaDialog.getInstance(mActivity , Constant.EnterPortStorage)
                        .setSingleSelect(true)
                        .setConfirmButton(new StorageAreaDialog.OnStorageOnClickListener() {
                            @Override
                            public void onClick(List<StorageAreaData> storageAreaList) {
                                startNFCIntent(storageAreaList.get(0).getId() , storageAreaList.get(0).getName() , Constant.Enter_Port_Area);
                            }
                        })
                        .show();
                break;
        }
    }


    private List<String> initChannelDataList(List<ChannelData> list){
        List<String> mList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            mList.add(list.get(i).getName());
        }

        return mList ;
    }

    private void showNFCDialog(List<String> dataList , int status , int type){
        NFCContentDialog.getInstance(mActivity , dataList)
                .setConfirmButton(new NFCContentDialog.OnStorageOnClickListener() {
                    @Override
                    public void onClick(int position) {
                        int ID = -1 ;
                        String code = "" ;
                        switch (status){
                            case 1:
                                ID = channelDataList.get(position).getId();
                                code = channelDataList.get(position).getCode();
                                break;
                        }

                        startNFCIntent(ID , code , type);
                    }
                }).show();
    }

    private void startNFCIntent(int ID , String code , int type){

        String value = ID != 0 ? JsonUtils.parseBeanToJson(new NfcBean(ID ,code , type)) : "";

        mIntent = new Intent(mActivity , NFCActivity.class);
        mBundle = new Bundle();
        mBundle.putString(Constant.Title , NfcUtils.getNfcTypeName(mActivity , type));
        mBundle.putString(Constant.Value , value);
        mIntent.putExtras(mBundle);
        turnActForResult(mIntent);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        if(resultCode == Constant.RESULT_NFC_CODE && requestCode == Constant.REQUEST_CODE){
            NfcBean nfcBean = (NfcBean) data.getExtras().getSerializable(Constant.INFO);

            AlertNFCDialog.getInstance(mActivity)
                    .setContent(nfcBean)
                    .show();
        }
    }
}
